
package com.dog.basics.service.wishwash.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.admin.wishwash.AdminDogHxOrderItmeRoleDto;
import com.dog.basics.bean.vo.admin.wishwash.AdminDogHxOrderItmeRoleVo;
import com.dog.basics.common.constant.CommonConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogConnectFlowingMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.bean.dto.admin.wishwash.AdminDogHxOrderItmeFromDto;
import com.dog.basics.bean.vo.admin.wishwash.AdminDogHxOrderItmeVo;
import com.dog.basics.bean.dto.api.wishwash.ApiDogHxOrderItmeAddDto;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.model.wishwash.DogHxOrderItme;
import com.dog.basics.dao.wishwash.DogHxOrderItmeMapper;
import com.dog.basics.service.wishwash.DogHxOrderItmeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 订单子项信息
 *
 * @author cy code generator
 * @date 2021-07-09 17:40:27
 */
@Service
@AllArgsConstructor
public class DogHxOrderItmeServiceImpl extends ServiceImpl<DogHxOrderItmeMapper, DogHxOrderItme> implements DogHxOrderItmeService {

    private final DogConnectFlowingMapper flowingMapper;

    /**
     * @Description: 后台管理分页
     * @Param: status
     * @return: IPage
     * @author cy code generator
     * @date 2021-07-09 17:40:27
     */
    @Override
    public IPage<AdminDogHxOrderItmeVo> getAdminPageList(PageParam page, AdminDogHxOrderItmeFromDto obj) {
        this.baseMapper.getAdminPageList(page, obj);
        return page;
    }


    /**
     * @Description: 增加APP
     * @Param: obj
     * @return: Boolean
     * @author cy code generator
     * @date 2021-07-09 17:40:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveApiObj(ApiDogHxOrderItmeAddDto obj) {
        // API - 数据信息转换
        DogHxOrderItme apiObjAdd = BeanUtil.toBean(obj, DogHxOrderItme.class);
        // 数据信息补充
        apiObjAdd.setCreateTime(DateUtil.currentSeconds());
        apiObjAdd.setCreateBy(SecurityUtils.getUser().getUserId());
        // 执行添加数据信息
        Validator.validateTrue(this.save(apiObjAdd), "保存失败!");
        return Boolean.TRUE;
    }

    /**
     * @功能描述: 订单管理
     * @author He
     * @date 2021/7/19
     */
    @Override
    public AdminDogHxOrderItmeRoleVo getDogHxOrderItmeRolePage(PageParam page, AdminDogHxOrderItmeRoleDto obj) {
        // 返回值声明
        AdminDogHxOrderItmeRoleVo orderItmeRoleVo = new AdminDogHxOrderItmeRoleVo();
        // 声明是否加入用户标识条件
        Boolean isUserId = Boolean.TRUE;
        // 判断当前是什么角色
        if (SecurityUtils.hasRole(CommonConstants.ROLE_AGENT_CODE)) {
            // 代理商
            obj.setAgentUserId(SecurityUtils.getSysUser().getUserId());
        } else if (SecurityUtils.hasRole(CommonConstants.ROLE_FIELD_CODE)) {
            // 场地方
            obj.setFileldUserId(SecurityUtils.getSysUser().getUserId());
        } else {
            isUserId = Boolean.FALSE;
        }
        // 订单分页查询
        this.baseMapper.getAdminPageRoleList(page, obj);
        orderItmeRoleVo.setPage(page);
        // 总收入查询
        BigDecimal sunUserOrderProfit = flowingMapper.getSunUserOrderProfit(isUserId ? SecurityUtils.getSysUser().getUserId() : null, obj.getStartTime(), obj.getEndTime());
        orderItmeRoleVo.setTotalRevenue(sunUserOrderProfit);
        return orderItmeRoleVo;
    }

}
